Blog

Curt Gratz

March 19, 2010

Spread the word


Share your thoughts

MockBox can have many amazing uses.  One well documented use is in assistance in creating complicated unit tests insuring you are only testing one small unit of work, however it can be used for many other interesting use cases.

One of the ways we find MockBox useful during our development cycle at Computer Know How is to mock objects we haven't had the time to complete yet, but we do know what we expect as response.  This allows us to continue development without waiting for the piece of something we haven't done yet slow us down, but keep the method calls exactly as they will be in when the object is complete.

So, how do you use MockBox outside of the context of a unit test?  Well, its easy.  

Inside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet inside a ColdBox app mockBox = createObject("component","coldbox.system.testing.MockBox").init();

Outside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet outside a ColdBox app. mockBox = createObject("component","mockbox.system.testing.MockBox").init();

Now that MockBox is initialized, we can start mocking objects. Lets say we have a User Object that we haven't had time to build yet. Right now it looks really advanced with lots of cool properties and functions that we spent a ton of time on. Something like this.

cfcomponent hint="I am the User ORM cfc" /cfcomponent

Now what if we needed to build a Welcome screen based off this User Object. Well, we could do something like this.

var rc = event.getCollection(); //get the users name, later we will need to switch this to get it from the user object. rc.Name = "Paul Was Saul"; But later, we would have to implement our user object once we are done with it. But what if we mocked our user object with the getName method it will ultimately have. var rc = event.getCollection(); //get an instance of mockbox var mockBox = createObject("component","coldbox.system.testing.MockBox").init() //Mock a User Object since its not built yet. var User=mockBox.createMock(className="model.ORM.User",clearMethods=true,callLogging=true); User.$(method="getName",returns="Paul Was Saul"); //get the users name rc.Name = User.getName();

Now, I know what your thinking. The non-mocked version looks a lot simpler then the mocked version. Well that is probably true in this simple example, but use your imagination and expand this example using the power of your mind and you can see how handy MockBox can be in your own development.

Want to learn more about MockBox. Read the wiki. As with everything done by Luis Majano, the documentation is outstanding.

http://wiki.coldbox.org/wiki/MockBox.cfm

Or Download it at

http://www.coldbox.org/download

Yet another tool to put in your toolbox to make your life simpler brought to you by Team ColdBox.

Add Your Comment

(2)

Mar 19, 2010 10:53:11 UTC

by Luis Majano

Cool approach Curt, My head started spinning and could be cool to have a mock interceptor that sets up all the mocks you would need for your session until objects get build out. This way you can put all your mock code in this single object and be coding it out. Very interesting approach. PS: you don't need to do: rc = event.getCollection() on your views/layouts. That's done implicitly :)

Mar 19, 2010 10:58:59 UTC

by Curt Gratz

Thanks man. In reality I do it in the mocks preHandler interceptor so its available to me when I need it. PS: those are are meant to be handlers, I don't think it does it implicitly there.

Recent Entries

TestBox Latest Updates and News!

TestBox Latest Updates and News!

We’re thrilled to have launched the new TestBox website and TestBox 6.0! If you haven’t had a chance to explore yet, visit TestBox to discover updated documentation, powerful resources, and features that make testing more efficient than ever.

Maria Jose Herrera
Maria Jose Herrera
November 21, 2024
Is Your ColdFusion Application Ready for the Future?

Is Your ColdFusion Application Ready for the Future?

In a rapidly evolving digital world, maintaining performance, security, and scalability for ColdFusion applications is more challenging than ever. Whether you're using Lucee or Adobe ColdFusion, legacy systems can become a bottleneck for growth, innovation, and user satisfaction. The need to future-proof your ColdFusion applications has never been more critical.

But where do you start?


The Hidden Costs of an Outdated ColdFusion Application

As you...

Cristobal Escobar
Cristobal Escobar
November 21, 2024
The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management


Opting for in-house database management involves more than just a salary. Here are some often-overlooked costs associated with maintaining your own DBA team.



1. High Salaries and Benefits


Hiring skilled DBAs is expensive. According to industry reports, the average salary of a DBA in the U.S. can range from $85,000 to over $130,000 per year, depending on experience and expertise. When you add ...

Cristobal Escobar
Cristobal Escobar
November 20, 2024